package org.car.world.common.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import freemarker.core.ParseException;

public class DateFormatUtil {
    /**
     * 日期减几年
     */
    public static String dateMinusYear(String str) throws Exception {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        Date dt = sdf.parse(str);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.YEAR, -1);// 日期减1年
        Date dt1 = rightNow.getTime();
        String reStr = sdf.format(dt1);
        return reStr;
    }

    /**
     * 日期加几年
     */
    public static String dateAddYear(String str) throws Exception {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        Date dt = sdf.parse(str);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.YEAR, 1);// 日期加1年
        Date dt1 = rightNow.getTime();
        String reStr = sdf.format(dt1);
        return reStr;
    }

    /**
     * 日期减几月
     */
    public static String dateMinusMonth(String str) throws Exception {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        Date dt = sdf.parse(str);//将字符串生成Date
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);//使用给定的 Date 设置此 Calendar 的时间。
        rightNow.add(Calendar.MONTH, -1);// 日期减1个月
        Date dt1 = rightNow.getTime();//返回一个表示此 Calendar 时间值的 Date 对象。
        String reStr = sdf.format(dt1);//将给定的 Date 格式化为日期/时间字符串，并将结果添加到给定的 StringBuffer。
        return reStr;
    }

    /**
     * 日期加几月
     */
    public static String dateAddMonth(String str) throws Exception {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        Date dt = sdf.parse(str);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.MONTH, 1);// 日期加3个月
        // rightNow.add(Calendar.DAY_OF_YEAR,10);//日期加10天
        Date dt1 = rightNow.getTime();
        String reStr = sdf.format(dt1);
        return reStr;
    }

    /**
     * 获取当前年月的第一个月的str
     *
     * @param str 201505
     * @return 201501
     * @throws Exception
     */
    public static String dateOneMonth(String str) {

        str = str.substring(0, str.length() - 2);
        str = str + "01";
        return str;
    }

    /**
     * 算出所选月份距离一月份有几个月。
     *
     * @param str 201509
     * @return 9
     */
    public static int dateDistanceMonth(String str) {

        int i = Integer.parseInt(str);
        int j = Integer.parseInt(DateFormatUtil.dateOneMonth(str));
        System.out.println(i - j);
        return i - j + 1;
    }

    /**
     * 获取两个时间的时间差，精确到毫秒
     *
     * @param str
     * @return
     */
    public static String TimeDifference(long start, long end, int typev) {

        String yearday = "";
        long newsjc = 0;
        long between = end - start;
        long day = between / (24 * 60 * 60 * 1000);
        long hour = (between / (60 * 60 * 1000) - day * 24);
        long min = ((between / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long s = (between / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        long ms = (between - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000
                - min * 60 * 1000 - s * 1000);
        if (typev == 0) {
            yearday = day + "天" + hour + "小时" + min + "分" + s + "秒" + ms
                    + "毫秒";
        } else if (typev == 1) {
            if (day % 365 == 0) {
                newsjc++;
                yearday = Long.toString(newsjc);
            }
            yearday = Long.toString(day);
        }
        return yearday;
    }

    /**
     * 判断dateStr是否在start和end中间，start和end都可以为null yyyyMMddHHmmss或者yyyyMMdd格式
     *
     * @param dateStr
     * @param start
     * @param end
     * @return
     * @throws java.text.ParseException
     * @author you.xu
     * @date 2015年8月19日下午3:11:46
     */
    public static boolean checkDateVal(String dateStr, String start, String end) throws java.text.ParseException, ParseException {
        boolean isDateRight = false;
        Date date = null;
        Date startDate = null;
        Date endDate = null;
        SimpleDateFormat sdf = null;
        // 判断日期格式
        if (14 == dateStr.length()) {
            sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        } else if (8 == dateStr.length()) {
            sdf = new SimpleDateFormat("yyyyMMdd");
        } else
            return false;

        // 更改判断日期格式
        date = sdf.parse(dateStr);

        if ((start == null) && (end != null)) {
            endDate = sdf.parse(end);
            if ((date != null) && (endDate != null))// Check parameters for
            {
                if (date.compareTo(endDate) <= 0)
                    isDateRight = true;
            }
        } else if ((start != null) && (end == null)) {
            startDate = sdf.parse(start);
            if ((date != null) && (startDate != null)) {
                if (date.compareTo(startDate) >= 0)
                    isDateRight = true;
            }
        } else if ((start != null) && (end != null)) {
            startDate = sdf.parse(start);
            endDate = sdf.parse(end);
            if ((startDate != null) && (date != null) && (endDate != null)) {
                if ((date.compareTo(startDate) >= 0)
                        && (date.compareTo(endDate) <= 0))
                    isDateRight = true;
            }
        }
        return isDateRight;
    }

    /**
     * 判断dateStr是否在start和end中间，start和end都可以为null long形格式
     *
     * @param dateStr
     * @param start
     * @param end
     * @return
     * @author you.xu
     * @date 2015年8月19日下午3:12:35
     */
    public static boolean checkDateV(String dateStr, String start, String end) {
        boolean isDateRight = false;
        long date = -1;
        long fromDate = -1;
        long toDate = -1;

        date = java.lang.Long.parseLong(dateStr);

        if ((start == null) && (end == null)) {
            isDateRight = true;
        } else if ((start == null) && (end != null)) {
            try {
                toDate = java.lang.Long.parseLong(end);
            } catch (NumberFormatException nfe) {
                System.err.println(nfe);
            }
            if (date <= toDate) {
                isDateRight = true;
            }
        } else if ((start != null) && (end == null)) {
            try {
                fromDate = java.lang.Long.parseLong(start);
            } catch (NumberFormatException nfe) {
                System.err.println(nfe);
            }

            if (date >= fromDate) {
                isDateRight = true;
            }
        } else if ((start != null) && (end != null)) {
            try {
                toDate = java.lang.Long.parseLong(end);
                fromDate = java.lang.Long.parseLong(start);
            } catch (NumberFormatException nfe) {
                System.err.println(nfe);
            }

            if ((date <= toDate) && (date >= fromDate)) {
                isDateRight = true;
            }
        }
        return isDateRight;
    }
}
